package com.xunshangwang.pinhaohuo.ui.fragments;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.xunshangwang.pinhaohuo.R;
import com.xunshangwang.pinhaohuo.entity.GoodsAttr;
import com.xunshangwang.pinhaohuo.http.HttpTool;
import com.xunshangwang.pinhaohuo.http.JsonHttpHandler;

import org.json.JSONObject;

import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * Created by xzp on 16/3/8.
 */
public class PinFragment extends BaseFragment implements View.OnClickListener{
    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private ImageView mRefreshBtn;
    private PinAdapter mAdapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_pin,container,false);
        mViewPager= (ViewPager) view.findViewById(R.id.group_pager);
        mTabLayout= (TabLayout) view.findViewById(R.id.group_attrs_tab);
        mRefreshBtn= (ImageView) view.findViewById(R.id.refresh_btn);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mAdapter=new PinAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        mRefreshBtn.setOnClickListener(this);
        mRefreshBtn.performClick();
    }
    @Override
    public void onClick(View v) {
        JsonHttpHandler<List<GoodsAttr>> handler=new JsonHttpHandler<List<GoodsAttr>>() {
            private ObjectAnimator mRefreshAnimator;
            @Override
            public void onStart() {
                super.onStart();
                if(mRefreshAnimator!=null && mRefreshAnimator.isRunning()){
                    mRefreshAnimator.cancel();
                }
                mRefreshBtn.setEnabled(false);
                mRefreshAnimator=ObjectAnimator.ofFloat(mRefreshBtn,"rotation",0,360);
                mRefreshAnimator.setDuration(800);
                mRefreshAnimator.setInterpolator(new LinearInterpolator());
                mRefreshAnimator.setRepeatCount(ValueAnimator.INFINITE);
                mRefreshAnimator.start();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                mRefreshBtn.setEnabled(true);
                mRefreshAnimator.cancel();
                mRefreshAnimator=null;
                mRefreshBtn.setRotation(0f);
            }

            @Override
            public List<GoodsAttr> parseData(JSONObject object) throws Throwable {
                return JSON.parseArray(object.getString("attrs"),GoodsAttr.class);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, List<GoodsAttr> response) {
                mAdapter.setList(response);
                mTabLayout.setTabsFromPagerAdapter(mAdapter);
                if(mAdapter.getCount()!=0){
                    mTabLayout.getTabAt(mViewPager.getCurrentItem()).select();
                }
            }
        };
        handler.setTag(getView());
        HttpTool.get(getContext(),"?g=Api&m=Extends&a=getGoodsAttrList",handler);
    }
    private class PinAdapter extends FragmentStatePagerAdapter{
        private List<GoodsAttr> mList;
        public PinAdapter(FragmentManager fm) {
            super(fm);
        }
        public List<GoodsAttr> getList(){
            return  mList;
        }

        public void setList(List<GoodsAttr> list) {
            this.mList = list;
            notifyDataSetChanged();
        }

        @Override
        public Fragment getItem(int position) {
            return PinGroupFragment.newInstance(getList().get(position).getAttr_id());
        }

        @Override
        public int getCount() {
            return mList==null?0:mList.size();
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mList.get(position).getAttr_name();
        }
    }
}
